package com.zyt.controller;

import com.alipay.api.AlipayApiException;
import com.sun.tools.corba.se.idl.StringGen;
import com.zyt.service.OrderService;
import com.zyt.util.AlipayTemplate;
import com.zyt.util.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@Controller
public class PayController {
    @Autowired
    private AlipayTemplate alipayTemplate;
    @Autowired
    private OrderService orderService;
    @RequestMapping("/pay")
    public void pay(HttpServletResponse response, String orderId, String amount) throws IOException, AlipayApiException {
//        设置相应格式
        response.setContentType("text/html;charset=UTF-8");

//private String out_trade_no; // 商户订单号 必填
//        private String subject; // 订单名称 必填
//        private String total_amount;  // 付款金额 必填
//        private String body; // 商品描述 可空


        PayVo payVo=new PayVo();
        payVo.setOut_trade_no(orderId);
        payVo.setSubject("支付页面");
        payVo.setTotal_amount(amount);
        payVo.setBody("当当支付");

//        发送支付请求
        String pay = alipayTemplate.pay(payVo);

//        通过response对象将内容展示到页面
        PrintWriter writer = response.getWriter();
        writer.println(pay);
    }

//    修改商品状态
    @RequestMapping("/orderPayed")
    public String orderPayed(int out_trade_no, HttpServletRequest request){
//        返回值为TRUE和false
//        true:支付宝发送的请求
//        false：不是支付宝发送的请求

        boolean b = alipayTemplate.rsaCheckV1(request);
        if(b){
            orderService.orderpayed(out_trade_no);
        }
        return "redirect://selectByUserId.do";
    }



}
